
import { getSjdxList, initSjdx, customReq, updateSjdx, insertSjdx } from "@/utils/net";
import ISjdxMode from "./ISjdxMode";
import lodash from 'lodash'

/**
 * @description: Home数据持久化实现层
 * @return {*}
 */
export default class SjdxModelImp implements ISjdxMode {

    private fields = {}
    private listOperation = {}
    getSjdxLbFields(): Array<any> {
        let list: any[] = []
        Object.keys(this.fields).map(item => {
            if (this.fields[item].lbzs === '1') {
                list.push(item)
            }
        })
        return list
    }

    getSjdxDetailFields(): Array<any> {
        let list: any[] = []
        Object.keys(this.fields).map(item => {
            if (this.fields[item].kzxxObj.cllxkz && this.fields[item].kzxxObj.cllxkz.dxjcxx && this.fields[item].kzxxObj.cllxkz.dxjcxx.show && this.fields[item].kjlx !== '$buttons' && this.fields[item].kjlx !== '$buttons') {
                list.push(item)
            }
        })
        return list
    }
    getSjdxUpdateFields(mode: string): { list: any, xzmrz: any } {
        let list: any[] = []
        let xzmrz: any = {}
        Object.keys(this.fields).map(item => {
            if (this.fields[item].kzxxObj.cllxkz && this.fields[item].kzxxObj.cllxkz[mode] && this.fields[item].kzxxObj.cllxkz[mode].show && this.fields[item].kjlx !== '$buttons' && this.fields[item].kjlx !== '$buttons') {
                list.push(item)
                if (this.fields[item].xzmrz) {
                    xzmrz[item] = this.fields[item].xzmrz
                }
            }
        })
        return { list: list, xzmrz: xzmrz }
    }

    getSjdxFields(): object {
        return this.fields
    }
    getSjdxList(sjdx: any, page: any, yobj: any, searchKey: string): Promise<any> {
        return new Promise((resolve, reject) => {
            getSjdxList(sjdx, page, yobj, searchKey).then((result) => {
                if (result.data.status) {
                    resolve(result.data.data);
                }
            }).catch((err) => {
                reject(new Error(err));
            })
        })
    }

    getListOperation(): Object {
        return this.listOperation
    }

    htqq(sjdxJcxx: any, cllx: string, ids: [], buttonOptions: any, editTableData?: []): Promise<any> {
        return new Promise((resolve, reject) => {
            let htqqcs = {
                sys: {
                    authCode: sjdxJcxx.sys.authCode,
                    cllx: cllx,
                    ids: ids,
                    editTableData: editTableData,
                    // changeCheckData: changeCheckData
                },
                // yobj: myData.selectReqData.yobj
            }
            htqqcs = lodash.merge(htqqcs, buttonOptions.params);
            customReq(htqqcs).then((result) => {
                if (result.data.status) {
                    uni.showToast({
                        title: result.data.msg,
                        icon: 'none'
                    })
                    resolve(result.data)
                }
            }).catch((err) => {
                reject(new Error(err));
            })
        })
    }
    initSjdx(dm: string): Promise<void> {
        return new Promise((resolve, reject) => {
            if (uni.getStorageSync(dm)) {
                if (uni.getStorageSync(dm).fields) {
                    this.fields = uni.getStorageSync(dm).fields
                    this.listOperation = uni.getStorageSync(dm).cllxkz.select
                }
                resolve(uni.getStorageSync(dm));
            }
            else {
                initSjdx(dm).then((result) => {
                    if (result.data.status) {
                        uni.setStorageSync(dm, result.data.data)
                        if (result.data.data.fields) {
                            this.fields = result.data.data.fields
                            this.listOperation = result.data.data.cllxkz.select
                        }
                        resolve(result.data.data);
                    }
                }).catch((err) => {
                    reject(new Error(err));
                })
            }
        })
    }
    updateSjdx(yobj: any, dxdm: string): Promise<void> {
        return new Promise((resolve, reject) => {
            updateSjdx(yobj, dxdm).then((result) => {
                if (result.data.status) {
                    uni.showToast({
                        title: result.data.msg,
                        icon: 'none',
                    })
                    resolve(result.data.data);
                }
            })
        })
    }
    insertSjdx(yobj: any, dxdm: string): Promise<void> {
        return new Promise((resolve, reject) => {
            insertSjdx(yobj, dxdm).then((result) => {
                if (result.data.status) {
                    uni.showToast({
                        title: result.data.msg,
                        icon: 'none'
                    })
                    resolve(result.data.data);
                }
            })
        })
    }
}
